home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10326 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: while loop problem
  5. Date: Sat, 16 Mar 96 20:36:09 GMT
  6. Organization: none
  7. Message-ID: <827008569snz@genesis.demon.co.uk>
  8. References: <Pine.OSF.3.91.960312133449.7844B-100000@io.UWinnipeg.ca> <Do6DB0.EtE@microunity.com> <Pine.A32.3.91.960312171258.149477C-100000@black.weeg.uiowa.edu> <DoBAC8.K3o@iquest.net>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <DoBAC8.K3o@iquest.net> dlmiller@iquest.net "Doug Miller" writes:
  15.  
  16. >The Amorphous Mass <robinson@blue.weeg.uiowa.edu> wrote:
  17. >
  18. >+On Tue, 12 Mar 1996, Tom Sanders wrote:
  19. >+
  20. >+> Bill Simpson <wsimpson@uwinnipeg.ca> writes:
  21. >+> |> Consider the following code fragment:
  22. >+> |>
  23. >+> |> y=2000; z=1000;
  24. >+> |> x=0;
  25. >+> |> while(x<=XMAX)
  26. >+> |>   {
  27. >+> |>   x+=(int) erand(mean);
  28. >+> |>   setdot(x,y,z);
  29. >+> |>   }
  30. >+> |>
  31. >+> |> The above code will also attempt to plot one point at an x value >XMAX
  32. >+> |> before it terminates.
  33. >+> |>
  34. >+> |> Is there a nice way to write the code so it works properly?
  35. >+>
  36. >+> y=2000; z=1000;
  37. >+> x=0;
  38. >+> do  {
  39. >+>      x+=(int) erand(mean);
  40. >+>      setdot(x,y,z);
  41. >+>      } while(x<=XMAX)
  42. >+
  43. >+  This has exactly the same problem as the original loop, because it adds
  44. >+erand() to x and calls setdot() _before_ checking x's value against XMAX.
  45. >+Also, you left off the semicolon after the while ().
  46. >+
  47. >+/**James Robinson***********************
  48. >
  49. >Well, let's give him a correct answer.
  50. >
  51. >In the *original* loop, just change the test (x <= XMAX) to (x < XMAX).
  52. >There.
  53.  
  54. How does that make it many more correct? I see nothing in the problem
  55. description to suggest that XMAX is not a valid value for x in a call to
  56. setdot() - it appears that it is valid. Even if you make that change to
  57. the original it doesn't cure the problem when erand() returns a number
  58. greater than 1 (I see nothing to suggest it can't).
  59.  
  60. > That wasn't so hard, was it?
  61.  
  62. Apparently so.
  63.  
  64. -- 
  65. -----------------------------------------
  66. Lawrence Kirby | fred@genesis.demon.co.uk
  67. Wilts, England | 70734.126@compuserve.com
  68. -----------------------------------------
  69.